define(["angularAMD", "moment"], function (angularAMD, moment) {
    angularAMD.service('F0301Service', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog', '$calculater',
        function ($rootScope, $filter, $translate, config, $MMWService, $mdDialog, $calculater) {
            this.init = function (options) {
                openDialog(options);

                function openDialog(data) {
                    $mdDialog.dialog(
                        'module/moduleF/program/F0301/F0301.html',
                        function (dialog) {
                            return {
                                css_url: 'module/moduleF/program/F0301/F0301.css',
                                wo_no: data.wo_no,
                                op_no: data.op_no,
                                machine_no: data.machine_no,
                                position_no: data.position_no,
                                materialItem: data.materialItem,
                                select_material_detail: [],
                                init: function() {
                                    if(dialog.position_no === "N/A"){
                                        dialog.position_no = $filter('translate')('F0301.NA');
                                    }
                                },
                                // 修改数量
                                input_material_qty: function (event, item) {
                                    var target = event.currentTarget;
                                    angular.element(target).addClass('is-focus');
                                    $calculater.open(function (num, dialogNum) {
                                        if (num == 0) {
                                            $rootScope.showAlert($filter('translate')('F0301.msg.qty_zero'));
                                            return;
                                        }
                                        if (num > item.origin_qty) {
                                            $rootScope.showAlert($filter('translate')('F0301.msg.qty_over', { origin_qty: item.origin_qty }));
                                            return;
                                        }
                                        angular.element(target).removeClass('is-focus');
                                        item.qty = num;
                                        dialogNum.hide();
                                    },
                                    function (dialogNum) {
                                        angular.element(target).removeClass('is-focus');
                                        dialogNum.hide();
                                    },
                                    {
                                        defaultNum: item.qty,
                                        decimal: true
                                    });
                                },
                                // 删除行
                                removeMaterialState: function (index) {
                                    dialog.select_material_detail.splice(dialog.select_material_detail.indexOf(dialog.select_material_detail[index]), 1);
                                },
                                // 确定
                                confirm: function(){
                                    if (dialog.select_material_detail.length == 0) {
                                        $rootScope.showAlert($filter('translate')('common.msg.plz_select_one'));
                                        return;
                                    }
                                    // 若is_need_check_lot = 1，则明细中物料批号必须不可以为N/A
                                    if (dialog.materialItem.is_need_check_lot == '1') {
                                        let tempList = dialog.select_material_detail.filter(s => s.material_lot_no == 'N/A');
                                        if (tempList.length > 0){
                                            $rootScope.showAlert($filter('translate')('F0301.msg.material_lot_no_cant_na'));
                                            return;
                                        }
                                    }
                                    var content = {
                                        equipment_no: dialog.machine_no,
                                        mo_no: dialog.wo_no,
                                        op_no: dialog.op_no,
                                        position_no: dialog.position_no == $filter('translate')('F0301.NA') ? 'N/A' : dialog.position_no,
                                        check_lot_no: dialog.materialItem.is_need_check_lot,
                                        materialList: dialog.select_material_detail
                                    }
                                    $MMWService.sendToServer({
                                        uri: 'ServicesSTD.Module_MAT.material_borrow_process',
                                        content: content,
                                        success: function (data) {
                                            $rootScope.showToast($filter('translate')('common.msg.success'));
                                            dialog.back();
                                        },
                                        error: function (data) {
                                            $rootScope.showAlert(data.Message);
                                        },
                                    });
                                },
                                // 点击+号
                                getMaterialState: function(){
                                    $mdDialog.dialog(
                                        'module/moduleF/program/F0301/F0301_material_state.html',
                                        dialog.MaterialStateController
                                    );
                                },
                                MaterialStateController: function (materialDialog) {
                                    return {
                                        css_url: 'module/moduleF/program/F0301/F0301.css',
                                        material_no: dialog.materialItem.material_no,
                                        material_lot_no: '',
                                        ori_mo_no: dialog.wo_no,
                                        material_detail: [],
                                        selectAll: false,
                                        // 查询
                                        search: function () {
                                            var content = {
                                                material_no: materialDialog.material_no,
                                                material_lot_no: materialDialog.material_lot_no,
                                                ori_mo_no: materialDialog.ori_mo_no
                                            }
                                            $MMWService.sendToServer({
                                                uri: 'ServicesSTD.Module_MAT.material_state_get',
                                                content: content,
                                                success: function (data) {
                                                    if (data.materialList.length == 0) {
                                                        $rootScope.showAlert($filter('translate')('common.msg.no_data'));
                                                        materialDialog.material_detail = [];
                                                    } else {
                                                        materialDialog.material_detail = data.materialList
                                                    }
                                                }
                                            });
                                        },
                                        // 全选
                                        selectAllData: function () {
                                            materialDialog.selectAll = !materialDialog.selectAll;
                                            materialDialog.material_detail.forEach(function (item) {
                                                item.isSelect = materialDialog.selectAll;
                                            });
                                            if (materialDialog.material_detail.length == 0) {
                                                materialDialog.selectAll = false;
                                            }
                                        },
                                        // 选择一行
                                        select: function (event, item) {
                                            event.stopPropagation();
                                            item.isSelect = !item.isSelect

                                            var checkarr = []
                                            materialDialog.material_detail.forEach(function (data) {
                                                if (data.isSelect) {
                                                    checkarr.push(data)
                                                }
                                            })
                                            if (checkarr.length === materialDialog.material_detail.length) {
                                                materialDialog.selectAll = true;
                                            } else {
                                                materialDialog.selectAll = false;
                                            }
                                        },
                                        // 确定
                                        confirm: function () {
                                            let selected = materialDialog.material_detail.filter(m => m.isSelect);
                                            if (selected.length == 0){
                                                $rootScope.showAlert($filter('translate')('common.msg.plz_select_one'));
                                                return;
                                            }
                                            selected.forEach(s => {
                                                if (dialog.select_material_detail.length > 0) {
                                                    let count = dialog.select_material_detail.filter(m => s.mo_no == m.mo_no && s.material_no == m.material_no && s.material_lot_no == m.material_lot_no && s.sub_stitute_material_no == m.sub_stitute_material_no);
                                                    if (count.length == 0) {
                                                        s.origin_qty = s.qty;
                                                        dialog.select_material_detail.push(s)
                                                    }
                                                } else {
                                                    s.origin_qty = s.qty;
                                                    dialog.select_material_detail.push(s)
                                                }
                                            });
                                            materialDialog.hide();
                                        },
                                        back: function () {
                                            materialDialog.hide();
                                        },
                                    }
                                },
                                back: function () {
                                    dialog.hide();
                                    if (options.callback)
                                        options.callback();
                                }
                                
                            };
                        }
                    );
                }


            };
        }
    ]);
});
